<template>
    <van-nav-bar title="收货地址" left-arrow @click-left="onClickLeft" />

    <van-address-list v-model="state.AddressId" :list="state.AddressList" default-tag-text="默认" @add="onAdd" @edit="onEdit"
        @select="onSelect" />
</template>
<script setup>
import { useRouter, useRoute } from 'vue-router';
import { reactive, onMounted } from 'vue'
import apiList from '@/api'
import { useUserStore } from '@/store/user.js'
import { storeToRefs } from 'pinia'
import { showNotify } from 'vant'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)

const state = reactive({
    AddressId: 0,// 选择地址
    AddressList: [],// 收货地址数据
})

const onClickLeft = () => {
    if (route.query.action == 'order' || route.query.action == 'productInfo') {
        router.back()
    } else {
        router.push('/business/base/index')
    }
};
const onAdd = () => {
    if (route.query.action == 'order' || route.query.action == 'productInfo') {
        let action = route.query.action
        router.push({ path: '/business/address/add', query: { action } });
        return//会往下执行，需要暂停
    }
    router.push('/business/address/add')
}
const onEdit = (item) => {
    if (route.query.action == 'order' || route.query.action == 'productInfo') {
        router.push({ path: '/business/address/edit', query: { id: item.id, busid: userInfo.value.id, action: route.query.action } });
        return
    }
    router.push({ path: '/business/address/edit', query: { id: item.id, busid: userInfo.value.id } });
}
onMounted(() => {
    getAddressData()
})

async function getAddressData() {

    let res = await apiList.AddressIndex({ busid: userInfo.value.id })
    if (res.code === 0) {
        showNotify({
            type: 'warning',
            duration: 1500,
            message: res.msg,
        })
    }

    let data = []

    for (let item of res.data) {
        if (item.status == 1) {
            state.AddressId = item.id
        }

        data.push({
            id: item.id,
            name: item.consignee,
            tel: item.mobile,
            address: item.region_text + ' ' + item.address,
            isDefault: item.status == 1 ? true : false
        })
    }

    state.AddressList = data

    //订单界面选择收获地址的默认选中
    if (route.query.addid) {
        state.AddressId = parseInt(route.query.addid)
    }
}

async function onSelect(info) {
    if (route.query.action == 'order' || route.query.action == 'productInfo') {
        // let data = {
        //     addrid: info.id,
        //     busid: userInfo.value.id
        // }
        let action = route.query.action
        router.push({ path: '/order/cart/confirm', query: { addrid: info.id, busid: userInfo.value.id, cartids: route.query.cartids, total: parseFloat(route.query.total),action } })
    } else {
        let data = {
            addrid: info.id,
            busid: userInfo.value.id
        }

        let result = await apiList.AddressSelect(data)

        if (result.code === 1) {
            getAddressData()
        } else {
            showNotify({
                type: 'warning',
                message: result.msg,
                duration: 1500,
            })
        }
    }
}

</script>